package com.foxcai.novel.action;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.foxcai.novel.base.ConfConstants;
import com.foxcai.novel.http.EasouHttpCommAction;
import com.foxcai.novel.util.Convert;

public class MakeHtml {

	public void MakeIndexImpl(String xml, int curPage, int totlePage) {

		Document doc = null;
		Element root = null;
		Element nodes = null;
		Element foo = null;
		StringBuffer novHtm = new StringBuffer();
		try {
			doc = DocumentHelper.parseText(xml);

			root = doc.getRootElement();
			nodes = root.element("data");

			if (root.elementText("success").equals("true")) {
				for (Iterator i = nodes.elementIterator("item"); i.hasNext();) {
					foo = (Element) i.next();
					novHtm.append("<dd>\r\n");
					novHtm.append("\t<span class=\"m2\">"
							+ foo.elementTextTrim("name") + "</span>\r\n");
					novHtm.append("\t<span class=\"m4\"><a href=\"mulu/mulu_"
							+ foo.elementText("gid") + ".html\">"
							+ foo.elementTextTrim("chapter_name")
							+ "</a></span>\r\n");
					novHtm.append("\t<span class=\"m6\">"
							+ Convert.TimeConvert(Long.parseLong(foo
									.elementText("time"))) + "</span>\r\n");
					novHtm.append("</dd>\r\n");

					makeMulu(foo.elementTextTrim("gid"), foo
							.elementTextTrim("name"));
				}

				String tempPath = "";
				String outPath = "";

				tempPath = ConfConstants.CONTEXT_PATH + "\\template\\index.htm";
				if (curPage == 1) {
					outPath = ConfConstants.CONTEXT_PATH + "\\index.html";
				} else {
					outPath = ConfConstants.CONTEXT_PATH + "\\index_" + curPage
							+ ".html";
				}

				String templateContent = "";
				FileInputStream fileinputstream = new FileInputStream(tempPath);// 读取模块文件
				int lenght = fileinputstream.available();
				byte bytes[] = new byte[lenght];
				fileinputstream.read(bytes);
				fileinputstream.close();
				templateContent = new String(bytes);
				templateContent = templateContent.replaceAll("##ListBody##",
						novHtm.toString());
				templateContent = templateContent.replaceAll("##TrunPage##",
						TrunPage(curPage, totlePage));

				FileOutputStream fileoutputstream = new FileOutputStream(
						outPath);// 建立文件输出流
				byte tag_bytes[] = templateContent.getBytes();
				fileoutputstream.write(tag_bytes);
				fileoutputstream.close();
				doc.clearContent();

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
	}

	public void makeMuluImpl(String xml, String gid, String name) {

		Document doc = null;
		Element root = null;
		Element nodes = null;
		Element foo = null;
		StringBuffer novHtm = new StringBuffer();
		try {
			doc = DocumentHelper.parseText(xml);

			root = doc.getRootElement();
			nodes = root.element("data");

			if (root.elementText("success").equals("true")) {
				for (Iterator i = nodes.elementIterator("item"); i.hasNext();) {
					foo = (Element) i.next();
					novHtm.append("<dd>\r\n");
					novHtm.append("\t<span class=\"m7\"><a href=\""
							+ foo.elementTextTrim("chapter_url")
							+ "&wver=c\" target=\"_blank\">"
							+ foo.elementTextTrim("chapter_name")
							+ "</a></span>\r\n");
					novHtm.append("\t<span class=\"m4\">"
							+ foo.elementTextTrim("site") + "</span>\r\n");
					novHtm.append("\t<span class=\"m6\">"
							+ Convert.TimeConvert(Long.parseLong(foo
									.elementText("time"))) + "</span>\r\n");
					novHtm.append("</dd>\r\n");
				}

				String tempPath = "";
				String outPath = "";

				tempPath = ConfConstants.CONTEXT_PATH + "\\template\\mulu.htm";
				outPath = ConfConstants.CONTEXT_PATH + "\\mulu\\mulu_" + gid
						+ ".html";

				String templateContent = "";
				FileInputStream fileinputstream = new FileInputStream(tempPath);// 读取模块文件
				int lenght = fileinputstream.available();
				byte bytes[] = new byte[lenght];
				fileinputstream.read(bytes);
				fileinputstream.close();
				templateContent = new String(bytes);
				templateContent = templateContent.replaceAll("##ListBody##",
						novHtm.toString());
				templateContent = templateContent.replaceAll("##NovelName##",
						name);
				templateContent = templateContent.replaceAll("##GID##", gid);
				// templateContent =
				// templateContent.replaceAll("##TrunPage##",TrunPage(curPage,
				// totlePage));

				FileOutputStream fileoutputstream = new FileOutputStream(
						outPath);// 建立文件输出流
				byte tag_bytes[] = templateContent.getBytes();
				fileoutputstream.write(tag_bytes);
				fileoutputstream.close();
				doc.clearContent();

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
	}

	public String TrunPage(int curPage, int totlePage) {

		StringBuffer pageHtm = new StringBuffer("翻页：&nbsp;");
		try {
			for (int i = 1; i <= totlePage; i++) {
				if (i == curPage) {
					pageHtm.append("第" + i + "页&nbsp;|&nbsp;");
					continue;
				}
				if (i == 1) {
					pageHtm.append("<a href=index.html>第" + i
							+ "页</a>&nbsp;|&nbsp;");
				} else {
					pageHtm.append("<a href=index_" + i + ".html>第" + i
							+ "页</a>&nbsp;|&nbsp;");
				}
			}
		} catch (Exception e) {
			return "";
		}
		return pageHtm.substring(0, pageHtm.lastIndexOf("|"));
	}

	public void makeIndex() {

		String reqUrl = "http://b1.easou.com/api/sogou/serial_novel.m";

		String reqParams = null;
		String resultXml = null;
		EasouHttpCommAction ehca = new EasouHttpCommAction();
		long beg = System.currentTimeMillis();

		for (int i = 1; i <= 8; i++) {
			System.out.println("第" + i + "页");
			reqParams = "cid=hsab&pageId=" + i;
			resultXml = ehca.send(reqParams, reqUrl);
			MakeIndexImpl(resultXml, i, 8);
		}
		long curTime = System.currentTimeMillis();
		long intvl = curTime - beg;
		System.out.println("耗时：" + intvl + "毫秒！！");
	}

	public void makeMulu(String gid, String name) {

		String reqUrl = "http://b1.easou.com/api/sogou/serial_chapter.m";

		String reqParams = null;
		String resultXml = null;
		EasouHttpCommAction ehca = new EasouHttpCommAction();
		reqParams = "cid=hsab&gid=" + gid + "&pageId=1";
		resultXml = ehca.send(reqParams, reqUrl);
		makeMuluImpl(resultXml, gid, name);
	}
}
